package wangwenjun.phase2.concurrencypattern.pipeline;

import lombok.extern.slf4j.Slf4j;
import util.ThreadUtil;

import java.util.concurrent.ThreadLocalRandom;

/**
 * @author ChangLiang
 * @date 2020/6/17
 */
@Slf4j
public class WorkerThread extends Thread {

    /**
     * 每个工人属于一个pipeline
     */
    private final ProductPipeline pipeline;

    public WorkerThread(String workerName, ProductPipeline pipeline) {
        super(workerName);
        this.pipeline = pipeline;
    }

    @Override
    public void run() {
        while (true) {
            Product product = pipeline.take();
            log.info("Thread:{} process product #{}",Thread.currentThread(),product.getProductId());
            product.create();
            ThreadUtil.sleepForSeconds(ThreadLocalRandom.current().nextInt(10));
        }
    }
}
